package com.gitee.cashzhang27.test.boot.crypt.mapper.config;


import com.gitee.cashzhang27.test.boot.crypt.mapper.util.CrudMapper;
import java.util.Properties;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/07/09 16:56
 */
@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MyBatisConfig {

  @Bean
  public MapperScannerConfigurer mapperScannerConfigurer() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    mapperScannerConfigurer.setBasePackage("com.gitee.cashzhang27.test.boot.crypt.mapper.mapper");

    Properties properties = new Properties();
    properties.setProperty("mappers", CrudMapper.class.getName());
    properties.setProperty("notEmpty", "false");
    properties.setProperty("IDENTITY", "MYSQL");
    properties.setProperty("ORDER", "BEFORE");

    MapperHelper mapperHelper = new MapperHelper();
    Config config = new Config();
    // 其他配置
    // 主键自增回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
    config.setOrder("BEFORE");
    mapperHelper.setConfig(config);

    mapperScannerConfigurer.setProperties(properties);
    mapperScannerConfigurer.setMapperHelper(mapperHelper);
    return mapperScannerConfigurer;
  }

}

